package cn.tedu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;


@Controller
public class RandomCode {

    @GetMapping("/randomCode")
    public void checkCode(HttpSession session, HttpServletResponse resp) throws IOException {
        int width = 100;
        int height = 40;
        //1.内存创建图片对象
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //2.绘制图片
        //2.1获取画笔对象
        Graphics graphics = bufferedImage.getGraphics();
        //2.2设置画笔颜色
        graphics.setColor(Color.white);
        //2.3填充背景颜色
        graphics.fillRect(0, 0, width - 1, height - 1);
        //设置画笔颜色
        graphics.setColor(Color.BLACK);
        //设置画笔大小
        graphics.setFont(new Font("微软雅黑", Font.PLAIN, 1));
        //2.4画边框
        graphics.drawRect(0, 0, width, height);
        Random random = new Random();
        //画干扰线
        for (int i = 0; i < 6; i++) {
            // 设置随机颜色
            graphics.setColor(getRandomColor());
            // 随机画线
            graphics.drawLine(random.nextInt(width), random.nextInt(height),
                    random.nextInt(width), random.nextInt(height));

        }

        //添加噪点
        for (int i = 0; i < 30; i++) {

            int x1 = random.nextInt(width);

            int y1 = random.nextInt(height);

            graphics.setColor(getRandomColor());

            graphics.fillRect(x1, y1, 2, 2);

        }

        //设置画笔大小
        graphics.setFont(new Font("微软雅黑", Font.BOLD, 22));
        // randomCode 用于保存随机产生的验证码
        StringBuffer randomCode = new StringBuffer();

        //2.5生产随机字符
        for (int i = 0; i < 4; i++) {
            //设置画笔颜色
            graphics.setColor(getRandomColor());

            //随机产生大小写字母和数字
            char capital = (char) (random.nextInt(26) + 'a');
            char letter = (char) (random.nextInt(26) + 'A');
            char num = (char) (random.nextInt(10) + '0');
            //将产生的随机数和字母存放在数组中
            char[] check = {capital, letter, num};
            //随机产生一个索引
            int index = (random.nextInt(check.length));
            //2.6将数组中的随机字符串写在图片上
            graphics.drawString(String.valueOf(check[index]), 25 * i + 6, 30);
            //保存随机产生的验证码字符串
            randomCode.append(check[index]);

        }
        System.out.println(randomCode.toString());
        //将验证码放入session作用域中
        session.setAttribute("randomCode", randomCode.toString());
        //3.输出图片到页面上
        //3.1获取输出流对象
        ServletOutputStream sos = resp.getOutputStream();
        // 禁止图像缓存
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        resp.setContentType("image/jpeg");
        //3.2将图片写在页面上
        //参数1:写出的图片  参数2:后缀 参数3:关联的输出流
        ImageIO.write(bufferedImage, "jpg", sos);
        //刷新
        sos.flush();
        //释放资源
        sos.close();
    }


    /**
     * 随机取色
     */
    private  Color getRandomColor() {
        Random random = new Random();
        // 产生随机的颜色分量来构造画笔的颜色值
        int red = random.nextInt(256);
        int green = random.nextInt(256);
        int blue = random.nextInt(256);
        return new Color(red, green, blue);
    }

}
